<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 *	mysql数据库操作类	 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id:$
 */
//@packet pf_load
class pf_mysql{

	/*{{{code*/
	const DM_NOT_SUPPERT_MYSQL =  '200001';	
	const DM_NOT_CONNECT_MYSQL =  '200002';	
	const DM_NOT_SELECT_DB = '200003';	
	const MYSQL_QUERY_FAIL = '200004';	
	const MYSQL_QUERY_GET_ALL = '200005';	
	const DB_LINK_LOSS = '200006';	
	const DB_A_QUERY_ID_LOSS = '200007';	
	const DB_O_QUERY_ID_LOSS = '200008';	
	const MYSQL_CLOSE_FAIL = '200009';	
	/*code}}}*/

	/*{{{member*/
	protected $__linkNum = 0;
	protected $__linkId = null;
	protected $__c = null;
	protected $__queryId = null;
	protected $_log = null;
	/*}}}*/

	/*{{{ function */
	/*{{{ function __construct()*/

	/**
	 * 构造函数-- 读取数据库配置信息
	 *
	 * @param array $conf 
	 */
	public function __construct($conf=null, $linkNum=0)
	{
		if (!extension_loaded('mysql')) {
			$argvs = array(
				'code' => self::DM_NOT_SUPPERT_MYSQL,
				'msg' => 'not suppert mysql!',
			);
			$this->set_error($argvs);
			return false;
		}		

		if (!empty($conf)) {
			$this->__c = $conf;
			$this->__linkNum = (int)$linkNum;
		}
		//加载日志类
		$this->_log = pf_load::lib('pf_log');
	}

	/*}}}*/
	/*{{{ public function connect()*/

	/**
	 * 数据库连接
	 *
	 *@param array $conf 数据库配置文件 , int $linkNum 数据库连接数
	 */
	public function __connect($linkNum=0, $conf=null)
	{
		if (null === $conf) {
			$conf = $this->__c;
		}

		$linkNum = (int)$linkNum;
		if (0 === $linkNum) {
			$linkNum = $this->__linkNum;
		}

		if (!($this->__linkId[$linkNum])){
			$conn = $conf['pconnect']?'mysql_pconnect':'mysql_connect';
			$this->__linkId[$linkNum] = @$conn($conf['hostname']. ':' . $conf['hostport'], $conf['user'], $conf['password']);
			if (false === $this->__linkId[$linkNum]) {
				$argvs = array(
						'code' => self::DM_NOT_CONNECT_MYSQL,
						'msg' => mysql_error(),
					);
				$this->set_error($argvs);
				return false;
			}
			$conf['dbname']= isset($conf['dbname'])?$conf['dbname']:'test';	
			if (!mysql_select_db($conf['dbname'], $this->__linkId[$linkNum])) {
				$argvs = array(
						'code' => self::DM_NOT_SELECT_DB,
						'msg' => mysql_error(),
					);
				$this->set_error($argvs);
				return false;
			}
			$dbVersion = mysql_get_server_info($this->__linkId[$linkNum]);
			if ($dbVersion >= "4.1") { 
				//使用UTF8存取数据库 需要mysql 4.1.0以上支持
				mysql_query("SET NAMES '".$conf['charset']."'", $this->__linkId[$linkNum]);
			}
			$this->__linkNum = $linkNum;
		}
		return true;
		
	}

	/*}}}*/
	/*{{{ function __query()*/

	/**
	 * 执行查询--不返回结果集
	 *
	 * @param string $sql  要执行的sql
	 *
	 * @return false | true
	 */
	public function __query($sql) 
	{
		if (!$this->__linkId[$this->__linkNum]) {
			$argvs = array(
				'code' => self::DB_LINK_LOSS,
				'msg' => 'db link loss',
				);
			$this->set_error($argvs);
			return false;
		}
		$this->__queryId = mysql_query($sql, $this->__linkId[$this->__linkNum]);
		if (false === $this->__queryId) 
		{
			$argvs = array(
				'code' => self::MYSQL_QUERY_FAIL,
				'msg' => mysql_error(),
				);
			$this->set_error($argvs);
			return false;
		}	
		return true;
	}

	/*}}}*/
	/*{{{ function __fetchAll()*/

	/**
	 * 获取所有的查询数据
	 *
	 * @param string $sql  要执行的sql
	 *
	 * @return array $res  结果集
	 */
	public function __fetchAll($sql=null)
	{
		if (!$this->__linkId[$this->__linkNum]) {
			return false;
		}
		if (null !== $sql) {
			if (false === $this->__query($sql)) {
				return false;
			}
		}
		if (!$this->__queryId) {
			$argvs = array(
				'code' => self::DB_A_QUERY_ID_LOSS,
				'msg' => 'fetchAll:query id is  loss',
				);
			$this->set_error($argvs);
			return false;
		}

		$list = array();
		while($res = mysql_fetch_assoc($this->__queryId)) {
			$list[] = $res;
		}
		mysql_data_seek($this->__queryId,0);
		return $list;
	}

	/*}}}*/
	/*{{{ function __fetch()*/

	/**
	 * 获取所有的查询数据-- 获取一条记录
	 *
	 * @param string $sql  要执行的sql
	 *
	 * @return array $res  结果集
	 */
	public function __fetch($sql=null)
	{
		if (!$this->__linkId[$this->__linkNum]) {
			return false;
		}

		if (null !== $sql) {
			if (false === $this->__query($sql)) {
				return false;
			}
		}

		if (!$this->__queryId) {
			$argvs = array(
				'code' => self::DB_O_QUERY_ID_LOSS,
				'msg' => 'fetch: query id is  loss',
				);
			$this->set_error($argvs);
			return false;
		}
		mysql_data_seek($this->__queryId,0);
		$res = mysql_fetch_assoc($this->__queryId);
		return $res;
	}

	/*}}}*/
	/*{{{ function __exec()*/

	/**
	 * 执行sql -- 针对insert delete update 等无需返回记录集的语句 
	 *
	 * @param string $sql  要执行的sql
	 *
	 * @return 成功 影响行数|失败 false 
	 */
	public function __exec($sql)
	{
		if (!$this->__linkId[$this->__linkNum]) {
			return false;
		}

		if (false === $this->__query($sql)) {
			$argvs = array(
					'code' => self::DB_O_QUERY_ID_LOSS,
					'msg' => 'fetch: query id is  loss',
					);
			$this->set_error($argvs);
			return false;
		}
		$this->__queryId = null;
		$res = array();
		$res['lastinsertid'] = mysql_insert_id($this->__linkId[$this->__linkNum]);
		$res['arows'] = mysql_affectpf_rows($this->__linkId[$this->__linkNum]);
		return $res;
	}

	/*}}}*/
	/*{{{ function __close()*/

	/**
	 * 关闭数据库 
	 *
	 * @return void 
	 */
	public function __close()
	{
		if (!empty($this->__queryId)) {
			mysql_free_result($this->__queryId);
		}
	
		if (!mysql_close($this->__linkId[$this->__linkNum])){
			$argvs = array(
				'code' => self::MYSQL_CLOSE_FAIL,
				'msg' => mysql_error(),
				);
			$this->set_error($argvs);
			return false;
		}
		unset($this->__linkId,$this->__queryId);
		return true;
	}

	/*}}}*/
	/*{{{ function __quote()*/

	/**
	 * 转义处理sql语句中的字符串
	 * 例子:此方法会把 abc'def 处理成 'abc\'def'
	 *
	 * @param string $str 需要转意的字符串
	 *
	 * @return void 
	 */
	public function __quote($str='') 
	{
		return mysql_escape_string($str);
	}

	/*}}}*/

	/**
	 * 设置错误
	 *
	 * @see pf_log.php
	 */
	private function set_error(array $argvs)
	{
		$this->_log->set_error($argvs);
	}

	/**
	 * 获取错误
	 *
	 * @see pf_log.php
	 */
	private function get_error()
	{
		return $this->_log->get_error();
	}
	/*}}}*/
}
